package com.lyl.quartz.jobs;

import com.lyl.quartz.service.HelloService;
import com.lyl.quartz.utils.SpringContextUtil;
import com.lyl.quartz.utils.TimeUtil;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.StringJoiner;

/**
 * @author lylbell
 * @date 2022-03-29 10:59
 */
public class HelloDataJob implements Job {

    /*//采用spring上下文进行注入
    HelloService helloService;

    @Resource
    SpringContextUtil springContextUtil;
*/

    private String name;

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void execute(JobExecutionContext context) {
        JobDetail jobDetail = context.getJobDetail();
        Trigger trigger = context.getTrigger();
        StringJoiner stringJoiner = new StringJoiner(" ")
                .add("Hello Job")
                .add(TimeUtil.parse(new Date()))
                .add(Thread.currentThread().getName())
                .add(context.getTrigger().getKey().getName());
        System.out.println(stringJoiner);
        System.out.println(jobDetail.getJobDataMap().get("job-key-1"));
        System.out.println(trigger.getJobDataMap().get("trigger-key-1"));

        // 若jobDetail和trigger同时设置了name，那么使用getMergedJobDataMap()方法时，会采用trigger的数据。
        System.out.println(context.getMergedJobDataMap().get("name"));
        System.out.println("set注入" + name);


        //手动注入
//       helloService = (HelloService)springContextUtil.applicationContext.getBean(StringUtils.uncapitalize(HelloService.class.getSimpleName()));
//        System.out.println(helloService);
    }
}
